﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——产品序列号
    /// </summary>
    public class SerialNumService : CanDoo.Data.Entity.Services.ServiceBase<SerialNum>, ISerialNumContract
    {
        public SerialNumService()
        {
        
        }

        /// <summary>
        /// 批量从销售退货单序列号中删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public OperationResult DeleteFromSellOutBackByIds(long[] ids)
        {
            OperationResult result = OperationResult.NoChanged;
            using (var scope = DbScopeFactory.Create())
            {
                foreach (var m in ids)
                    DeleteFromSellOutBack(m);
                return scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged;
            }
        }

        /// <summary>
        /// 从销售退货单序列号中删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult DeleteFromSellOutBack(long id)
        {
            OperationResult result = OperationResult.NoChanged;
            using (var scope = DbScopeFactory.Create())
            {
                var mEntity = GetById(id);
                if (mEntity != null)
                {
                    if (mEntity.Status != 6)
                    {
                        throw new Exception("数据[" + id + "]状态不正确，不允许删除");
                    }
                    else
                    {
                        mEntity.Status = 5;
                        mEntity.OutSellBackId = null;
                        return UpdateEntity(mEntity);
                    }
                }
                else
                {
                    throw new Exception("数据[" + id + "]不存在，可能已删除");
                }
            }
        }




        /// <summary>
        /// 批量从销售出库单序列号中删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public OperationResult DeleteFromSellOutStoreByIds(long[] ids)
        {
            OperationResult result = OperationResult.NoChanged;
            using (var scope = DbScopeFactory.Create())
            {
                foreach (var m in ids)
                    DeleteFromSellOutStore(m);
                return scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged;
            }
        }

        /// <summary>
        /// 从销售出库单序列号中删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult DeleteFromSellOutStore(long id)
        {
            OperationResult result = OperationResult.NoChanged;
            using (var scope = DbScopeFactory.Create())
            {
                var mEntity = GetById(id);
                if (mEntity != null)
                {
                    if (mEntity.Status != 4)
                    {
                        throw new Exception("数据[" + id + "]状态不正确，不允许删除");
                    }
                    else
                    {
                        if (mEntity.OutSellBackId != null)
                            mEntity.Status = 7;
                        else
                            mEntity.Status = 1;
                        mEntity.OutStoreId = null;
                        mEntity.OutStoreTypeId = null;
                        return UpdateEntity(mEntity);
                    }
                }
                else
                {
                    throw new Exception("数据[" + id + "]不存在，可能已删除");
                }
            }
        }

        /// <summary>
        /// 批量从采购退货单序列号中删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public OperationResult DeleteFromInBuyBackByIds(long[] ids)
        {
            OperationResult result = OperationResult.NoChanged;
            using (var scope = DbScopeFactory.Create())
            {
                foreach (var m in ids)
                    DeleteFromInBuyBack(m);
                return scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged;
            }
        }

        /// <summary>
        /// 从采购退货单序列号中删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult DeleteFromInBuyBack(long id)
        {
            OperationResult result = OperationResult.NoChanged;
            using (var scope = DbScopeFactory.Create())
            {
                var mEntity = GetById(id);
                if (mEntity != null)
                {
                    if (mEntity.Status != 2)
                    {
                        throw new Exception("数据[" + id + "]状态不正确，不允许删除");
                    }
                    else
                    {
                        mEntity.Status =1;
                        mEntity.InBuyBackId = null;
                        return UpdateEntity(mEntity);
                    }
                }
                else
                {
                    throw new Exception("数据[" + id + "]不存在，可能已删除");
                }
            }
        }
    }
}
